# $Id$

include ../config/Makefile

SHELL=/bin/sh

INCLUDES=-I ../odyl -I ../boot -I $(OTOP)/utils -I $(OTOP)/parsing -I $(OTOP)/otherlibs/dynlink
OCAMLCFLAGS= $(INCLUDES) -warn-error A $(INCLUDES)
LINKFLAGS=$(INCLUDES)
INTERFACES=-I $(OLIBDIR) Arg Array ArrayLabels Buffer Callback CamlinternalOO Char Complex Digest Filename Format Gc Genlex Hashtbl Int32 Int64 Lazy Lexing List ListLabels Map Marshal MoreLabels Nativeint Obj Oo Parsing Pervasives Printexc Printf Queue Random Scanf Set Sort Stack StdLabels Stream String StringLabels Sys Weak -I ../boot Extfold Extfun Fstream Gramext Grammar Plexer Stdpp Token -I $(OTOP)/utils Config Warnings -I $(OTOP)/parsing Asttypes Location Longident Parsetree -I . Ast2pt MLast Pcaml Quotation Spretty
CAMLP4_INTF=$(OTOP)/utils/config.cmi $(OTOP)/utils/warnings.cmi $(OTOP)/parsing/asttypes.cmi $(OTOP)/parsing/location.cmi $(OTOP)/parsing/longident.cmi $(OTOP)/parsing/parsetree.cmi ast2pt.cmi mLast.cmi pcaml.cmi spretty.cmi quotation.cmi
CAMLP4_OBJS=../boot/stdpp.cmo ../boot/token.cmo ../boot/plexer.cmo ../boot/gramext.cmo ../boot/grammar.cmo ../boot/extfold.cmo ../boot/extfun.cmo ../boot/fstream.cmo $(OTOP)/utils/config.cmo quotation.cmo spretty.cmo reloc.cmo pcaml.cmo ast2pt.cmo argl.cmo
CAMLP4_XOBJS=../lib/stdpp.cmx ../lib/token.cmx ../lib/plexer.cmx ../lib/gramext.cmx ../lib/grammar.cmx ../lib/extfold.cmx ../lib/extfun.cmx ../lib/fstream.cmx $(OTOP)/utils/config.cmx quotation.cmx spretty.cmx reloc.cmx pcaml.cmx ast2pt.cmx argl.cmx
OBJS=../odyl/odyl.cma camlp4.cma
CAMLP4M=

CAMLP4=camlp4$(EXE)
CAMLP4OPT=phony

all: $(CAMLP4)

opt: opt$(PROFILING)

optnoprof: $(OBJS:.cma=.cmxa)

optprof: optnoprof $(OBJS:.cma=.p.cmxa)

optp4: $(CAMLP4OPT)

$(CAMLP4): $(OBJS) ../odyl/odyl.cmo
	$(OCAMLC) -linkall -o $@ $(OBJS) $(CAMLP4M) ../odyl/odyl.cmo

$(CAMLP4OPT): $(OBJS:.cma=.cmxa) ../odyl/odyl.cmx
	$(OCAMLOPT) -linkall -o $@ $(OBJS:.cma=.cmxa) $(CAMLP4M) ../odyl/odyl.cmx

$(OTOP)/utils/config.cmx: $(OTOP)/utils/config.ml
	$(OCAMLOPT) -I $(OTOP)/utils -c $(OTOP)/utils/config.ml

$(OTOP)/utils/config.p.cmx: $(OTOP)/utils/config.ml
	$(OCAMLOPT) -I $(OTOP)/utils -p -c -o $@ $(OTOP)/utils/config.ml

camlp4.cma: $(CAMLP4_OBJS)
	$(OCAMLC) $(LINKFLAGS) -a -o $@ $(CAMLP4_OBJS)

camlp4.cmxa: $(CAMLP4_XOBJS)
	$(OCAMLOPT) $(LINKFLAGS) -a  -o $@ $(CAMLP4_XOBJS)

camlp4.p.cmxa: $(CAMLP4_XOBJS:.cmx=.p.cmx)
	$(OCAMLOPT) $(LINKFLAGS) -a  -o $@ $(CAMLP4_XOBJS:.cmx=.p.cmx)

clean::
	rm -f *.cm* *.pp[io] *.$(O) *.$(A) *.bak .*.bak *.out *.opt
	rm -f $(CAMLP4)

depend:
	cp .depend .depend.bak
	> .depend
	@for i in *.mli *.ml; do \
	  ../tools/apply.sh pr_depend.cmo -- $(INCLUDES) $$i | \
	  sed -e 's| \.\./\.\.| $$(OTOP)|g' >> .depend; \
	done

promote:
	cp $(CAMLP4) ../boot/.

compare:
	@for j in $(CAMLP4); do \
		if cmp $$j ../boot/$$j; then :; else exit 1; fi; \
	done

install:
	-$(MKDIR) "$(BINDIR)"
	-$(MKDIR) "$(LIBDIR)/camlp4"
	cp $(CAMLP4) "$(BINDIR)/."
	cp mLast.mli quotation.mli ast2pt.mli pcaml.mli spretty.mli "$(LIBDIR)/camlp4/."
	cp mLast.cmi quotation.cmi ast2pt.cmi pcaml.cmi spretty.cmi "$(LIBDIR)/camlp4/."
	cp  argl.cmi argl.cmo "$(LIBDIR)/camlp4/."
	for f in argl.o argl.cmx; do \
	  if test -r $$f ; then \
		cp $$f "$(LIBDIR)/camlp4/." ; \
	  fi ; \
	done
	cp camlp4.cma $(LIBDIR)/camlp4/.
	for f in  camlp4.$(A)  camlp4.p.$(A) ; do \
	  if test -f $$f ; then \
	     cp $$f "$(LIBDIR)/camlp4/." && ( cd  "$(LIBDIR)/camlp4/." && $(RANLIB) $$f ) ; \
	  fi ; \
	done
	for f in camlp4.cmxa camlp4.p.cmxa ; do \
	  if test -f $$f ; then \
	     cp $$f "$(LIBDIR)/camlp4/." ; \
	  fi ; \
	done

include .depend
